package com.example.android_app.network.services.consmp;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

import com.example.android_app.GameActivity;
import com.example.android_app.network.Constants;
import com.example.android_app.network.ServiceCall;
import com.pervasive.project.unified.data.model.request.StartGameRequest;
import com.pervasive.project.unified.data.model.response.StartGameResponse;

public class StartGameRequestService implements ServiceCall{

	private HttpPut request;
	private DefaultHttpClient httpclient;
	private GameActivity gameActivity;
	private StartGameRequest startGameRequest;
	
	public StartGameRequestService(DefaultHttpClient httpclient, GameActivity gameActivity, StartGameRequest startGameRequest) {
		// TODO Auto-generated constructor stub
		this.request = new HttpPut("http://"+Constants.SERVICE_HOST+":"+Constants.SERVICE_PORT+"/services/rest/services/consumeStartGameRequestService");
		request.addHeader("Accept", "text/xml");
		this.httpclient = httpclient;
		this.gameActivity = gameActivity;
		this.startGameRequest = startGameRequest;
	}
	
	private static String read(InputStream instream) 
    {
        StringBuilder sb = null;
        try 
        {
             sb = new StringBuilder();
             BufferedReader r = new BufferedReader(new InputStreamReader(instream));
             for (String line = r.readLine(); line != null; line = r.readLine()) 
             {
             	sb.append(line);
             }

			instream.close();

        } catch (IOException e) 
        {
        	
        }
        return sb.toString();
    }
	
	@Override
	public void call() {
		// TODO Auto-generated method stub
		HttpResponse response = null;
		Serializer serializer = new Persister();
		OutputStream outputStream = new ByteArrayOutputStream();
		
		try {
			serializer.write(startGameRequest, outputStream);
			request.setEntity(new StringEntity(outputStream.toString()));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			response = httpclient.execute(request);
			HttpEntity httpEntity = response.getEntity();
			InputStream inputStream = null;
			inputStream = httpEntity.getContent();
			String jaxrsmessage = read(inputStream);
			System.out.println(">>>>>>>>"+jaxrsmessage);
			
			serializer = new Persister();       
			Reader reader = new StringReader(jaxrsmessage);
			StartGameResponse gameResponse = new StartGameResponse();
			gameResponse = serializer.read(StartGameResponse.class, reader, false);
			
			gameActivity.updateStartGameInfo(gameResponse);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			System.out.println(" >>>>>>>>>>>>>>>>>>> ClientProtocolException Occured <<<<<<<<<<<<<<<<<<<<< ");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			System.out.println(" >>>>>>>>>>>>>>>>>>> IOException Occured <<<<<<<<<<<<<<<<<<<<< ");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
